package com.wmb.permissionservice.service.Impl;

import com.wmb.permissionservice.mapper.UserPermissionMapper;
import com.wmb.permissionservice.model.pojo.UserRole;
import com.wmb.permissionservice.service.UserPermissionService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class UserPermissionServiceImpl implements UserPermissionService {

    // 角色码 role_code
    private static final String SUPER_ADMIN = "super_admin";
    private static final String USER = "user";
    private static final String ADMIN = "admin";

    private UserPermissionMapper userPermissionMapper;


    /**
     * 绑定默认角色（普通用户）
     * @param userId
     * @return
     */
    @Override
    public Integer bindDefaultRole(Long userId) {
        // 先获取普通用户的 roleId
        Integer roleId = userPermissionMapper.getRoleId(USER);
        // 把新用户绑定为普通用户
        return userPermissionMapper.insertUser(new UserRole(userId,roleId));
    }

    /**
     * 获取用户角色权限
     * @param userId
     * @return
     */
    @Override
    public Integer getUserRoleId(Long userId) {
        return userPermissionMapper.getRoleIdByUserId(userId);
    }


    /**
     * 修改用户角色权限-- superAdmin专用
     * @param userId
     * @param userRoleCodeNew
     * @return
     */
    @Override
    public Integer updateUserRole(Long userId, String userRoleCodeNew) {
        userPermissionMapper.updateUserRole(userId, userRoleCodeNew);
        return null;
    }

    /**
     * 鉴权
     * @param loginUserId
     * @param checkoutId
     * @return
     */
    @Override
    public Boolean permissionCheckout(Long loginUserId, Long checkoutId) {
        Integer loginRoleId = userPermissionMapper.getRoleIdByUserId(loginUserId);
        Integer checkoutRoleId = userPermissionMapper.getRoleIdByUserId(checkoutId);
        if (loginRoleId == 1) {
            return true;
        }else if (loginRoleId == 3) {
            return checkoutRoleId != 1;
        }else {
            return checkoutRoleId != 1 && checkoutRoleId != 3;
        }
    }

}
